package com.ouman.luoliluoli.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ouman.luoliluoli.LuoliDetailActivity;
import com.ouman.luoliluoli.R;
import com.ouman.luoliluoli.models.LuoliTalkModel;
import com.ouman.luoliluoli.utils.GlobalConstants;
import com.ouman.luoliluoli.utils.TimeDisplayHelper;

import java.util.List;

/**
 * Created by jintian on 26/05/2017.
 */

public class LuoliTalkRecyclerViewAdapter extends RecyclerView.Adapter<LuoliTalkRecyclerViewAdapter.ViewHolder> {


    private Context context;
    private List<LuoliTalkModel> data;
    public LuoliTalkRecyclerViewAdapter(Context context, List<LuoliTalkModel> data){
        this.context = context;
        this.data = data;
    }

    public LuoliTalkRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_item_talk, null);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("===>>click!!!!!");
            }
        });

        return new LuoliTalkRecyclerViewAdapter.ViewHolder(view);
    }

    public void onBindViewHolder(final LuoliTalkRecyclerViewAdapter.ViewHolder holder, final int position){


        String authorAvatarUrl = data.get(position).getNews_author_avatar_url();
        String title = data.get(position).getNews_title();
        String authorName =data.get(position).getNews_author_name();
        String authorSign = data.get(position).getNews_author_sign();
        String postTime = TimeDisplayHelper.displayTime(data.get(position).getNews_post_time());

        String newsHeadImageUrl = GlobalConstants.API_BASE_URL + data.get(position).getNews_head_image_url();
        System.out.println("news head url: " + newsHeadImageUrl);

        Animation animation = AnimationUtils.loadAnimation(context, R.anim.anim_card_show);
        holder.cardViewTalk.startAnimation(animation);


        Glide.clear(holder.imageViewAuthorAvatar);
        Glide.with(context)
                .load(authorAvatarUrl)
                .asBitmap()
                .error(R.drawable.avatar1)
                .into(holder.imageViewAuthorAvatar);

        Glide.clear(holder.imageViewNewsHeadImage);
        Glide.with(context)
                .load(newsHeadImageUrl)
                .asBitmap()
                .error(R.drawable.real_random_2)
                .into(holder.imageViewNewsHeadImage);

        holder.textViewTitle.setText(title);
        holder.textViewAuthorName.setText(authorName);
        holder.textViewAuthorSign.setText(authorSign);
        holder.textViewTime.setText(postTime);

        holder.cardViewTalk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(view.getContext(), LuoliDetailActivity.class);
                intent.putExtra("talkId", data.get(position).getNews_id());
                System.out.println(data.get(position).getNews_id());
                context.startActivity(intent);
            }
        });

    }

    public int getItemCount(){
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView imageViewAuthorAvatar;
        TextView textViewTitle;
        TextView textViewAuthorName;
        TextView textViewAuthorSign;
        TextView textViewTime;
        ImageView imageViewNewsHeadImage;
        CardView cardViewTalk;

        public ViewHolder(View view){
            super(view);
            imageViewAuthorAvatar = (ImageView) view.findViewById(R.id.imageViewAuthorAvatar);

            textViewTitle = (TextView) view.findViewById(R.id.textViewTitle);
            textViewAuthorName = (TextView) view.findViewById(R.id.textViewAuthorName);
            textViewAuthorSign = (TextView) view.findViewById(R.id.textViewAuthorSign);
            textViewTime = (TextView) view.findViewById(R.id.textViewTime);

            imageViewNewsHeadImage = (ImageView) view.findViewById(R.id.imageViewNewsHeadImage);

            cardViewTalk = (CardView) view.findViewById(R.id.cardViewTalk);
        }

    }
}
